﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YLW.Plat.Jhfw.HL7V3.XSDModels.PRPA_IN900300UV_Mzgh;
using YLW.Plat.Jhfw.Model.JhfwMzgh;

namespace YLW.Plat.Jhfw.HL7V3.V3Message
{
    public class V3_PRPA_IN900300UV_Mzgh
    {
        public static string OutPatientInfoQuery(GetJhfwMzghListIn jsonData, string id_extension, string sender_name, string receiver_name)
        {
            string hl7Xml = string.Empty;

            if (jsonData == null)
            {
                return hl7Xml;
            }

            //根据XML XSD构建HL7 XML消息格式

            //创建根对象
            var xmlRoot = new PRPA_IN900300UV
            {
                ITSVersion = "XML_1.0",
                id1 = new id1 { root = "2.16.156.10011.2.5.1.1", extension = id_extension },
                creationTime = new creationTime { value = DateTime.Now.ToString("yyyyMMddHHmmss") },
                interactionId = new interactionId { root = "2.16.156.10011.2.5.1.2", extension = "PRPA_IN900300UV" },
                processingCode = new processingCode { code = "P" },
                processingModeCode = new processingModeCode(),
                acceptAckCode = new acceptAckCode { code = "AL" },
                receiver = new receiver
                {
                    typeCode = "RCV",
                    device1 = new device1
                    {
                        classCode = "DEV",
                        determinerCode = "INSTANCE",
                        id1 = new id1
                        {
                            item = new item
                            {
                                root = "2.16.156.10011.2.5.1.3",
                                extension = receiver_name
                            }
                        }
                    }
                },
                sender = new sender
                {
                    typeCode = "SND",
                    device1 = new device1
                    {
                        classCode = "DEV",
                        determinerCode = "INSTANCE",
                        id1 = new id1
                        {
                            item = new item
                            {
                                root = "2.16.156.10011.2.5.1.3",
                                extension = sender_name
                            }
                        }
                    }
                },
                controlActProcess = new controlActProcess
                {
                    classCode = "ACTN",
                    moodCode = "EVN",
                    queryByParameter = new queryByParameter
                    {
                        queryId = new queryId { extension = "" },
                        statusCode = new statusCode(),
                        careEventID = new value
                        {
                            value1 = new value1
                            {
                                Items = new object[] {
                                    new item{
                                        root = "2.16.156.10011.1.11",
                                        extension = ""
                                    },
                                    new item{
                                        root = "2.16.156.10011.2.5.1.8",
                                        extension = jsonData.series
                                    },
                                    new item{
                                        root = "2.16.156.10011.2.5.1.9",
                                        extension = jsonData.jzlsh
                                    }
                                }
                            }
                        },
                        encounterTimeframe = new value
                        {
                            value1 = new value1
                            {
                                Items = new object[] {
                                    new low{ value=jsonData.jz_date_low },
                                    new high{ value=jsonData.jz_date_high },
                                }
                            }
                        },
                        patientId = new value
                        {
                            value1 = new value1
                            {
                                Items = new object[] {
                                    new item
                                    {
                                        root = "2.16.156.10011.2.5.1.4",
                                        extension = jsonData.mrn
                                    },
                                    new item
                                    {
                                        root = "2.16.156.10011.1.3",
                                        extension = jsonData.pcid
                                    }
                                }
                            }
                        },
                        patientLocationID = new value
                        {
                            value1 = new value1
                            {
                                Items = new object[] {
                                    new item
                                    {
                                        root = "2.16.156.10011.1.26",
                                        extension = jsonData.jzksdm
                                    }
                                }
                            }
                        },
                        responsibleOrganization = new value
                        {
                            value1 = new value1
                            {
                                Items = new object[] {
                                    new item
                                    {
                                        root = "2.16.156.10011.1.5",
                                        extension = ""
                                    }
                                }
                            }
                        },
                        typeOfEncounter = new value
                        {
                            value1 = new value1
                            {
                                Items = new object[] {
                                    new item
                                    {
                                        code = "1",
                                        codeSystem = "2.16.156.10011.2.3.1.271",
                                        codeSystemName = "患者类型代码表",
                                        displayName = new displayName { value = "门急诊" }
                                    }
                                }
                            }
                        },
                        extension = new extension
                        {
                            page = new value
                            {
                                value1 = new value1
                                {
                                    Text = new string[] { jsonData.page.ToString() }
                                }
                            },
                            percount = new value
                            {
                                value1 = new value1
                                {
                                    Text = new string[] { jsonData.percount.ToString() }
                                }
                            },
                            hoscode = new value
                            {
                                value1 = new value1
                                {
                                    Text = new string[] { jsonData.hoscode }
                                }
                            },
                            jzlsh = new value
                            {
                                value1 = new value1
                                {
                                    Text = new string[] { jsonData.jzlsh }
                                }
                            },
                            mrn = new value
                            {
                                value1 = new value1
                                {
                                    Text = new string[] { jsonData.mrn }
                                }
                            },
                            pcid = new value
                            {
                                value1 = new value1
                                {
                                    Text = new string[] { jsonData.pcid }
                                }
                            },
                            series = new value
                            {
                                value1 = new value1
                                {
                                    Text = new string[] { jsonData.series }
                                }
                            },
                            stay = new value
                            {
                                value1 = new value1
                                {
                                    Text = new string[] { jsonData.stay }
                                }
                            },
                            jzksdm = new value
                            {
                                value1 = new value1
                                {
                                    Text = new string[] { jsonData.jzksdm }
                                }
                            },
                            jz_date_low = new value
                            {
                                value1 = new value1
                                {
                                    Text = new string[] { jsonData.jz_date_low }
                                }
                            },
                            jz_date_high = new value
                            {
                                value1 = new value1
                                {
                                    Text = new string[] { jsonData.jz_date_high }
                                }
                            }
                        }
                    }
                }
            };

            hl7Xml = HL7V3Helper.SerializeToXml(xmlRoot);

            return hl7Xml;
        }
    }
}
